package com.hengpeng.itfinoss.tags;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.acl.vo.Passport;
import com.hengpeng.common.constant.Constant;

/**
 * <pre>
 * 	查询权限
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2011-8-18
 */
public class SearchPermissionsTag extends TagSupport
{

	private static final long serialVersionUID = 2064583312370360584L;

	private final static Log log = LogFactory.getLog(SearchPermissionsTag.class);

	/**
	 * 集合
	 */
	private String collection;

	/**
	 * 选中id
	 */
	private String selId;

	/**
	 * 类型
	 */
	private String type;

	public String getCollection()
	{
		return collection;
	}

	public void setCollection(String collection)
	{
		this.collection = collection;
	}

	public String getSelId()
	{
		return selId;
	}

	public void setSelId(String selId)
	{
		this.selId = selId;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	public int doStartTag() throws JspException
	{
		super.doStartTag();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		try
		{
			pageContext.getOut().print(getSelectStr(request));

		}
		catch (Exception e)
		{
			log.error("do SearchByStoreTag failed, page:" + ((Servlet) this.pageContext.getPage()).toString()
					+ ", falt detail:" + e.toString(), e);
		}

		return SKIP_BODY;
	}

	public String getSelectStr(HttpServletRequest request) throws ClassNotFoundException, IllegalArgumentException,
			IllegalAccessException
	{
		Passport passport = (Passport) request.getSession().getAttribute(Constant.PROFILE_SESSION_NAME);
		if (passport == null)
			return "";

		Object selObjectId = request.getParameter(selId);
		Object attribute = request.getAttribute(collection);
		Object id = -1;
		StringBuilder selectSb = new StringBuilder();
		try
		{
			Field selectIdField = passport.getClass().getDeclaredField(selId);
			selectIdField.setAccessible(true);
			id = selectIdField.get(passport);
			if (id == null)
			{
				id = -1;
			}
		}
		catch (Exception e)
		{
			log.error("获取会员店标识异常", e);
		}

		if (Collection.class.isAssignableFrom(attribute.getClass()))
		{
			Class<?> type = Class.forName(this.type);
			List<Field> allField = new ArrayList<Field>();
			// 父类的方法
			while (!type.equals(Object.class))
			{
				Field[] fields = type.getDeclaredFields();
				allField.addAll(Arrays.asList(fields));
				type = type.getSuperclass();
			}
			Field sId = null;
			Field sName = null;
			for (Field field : allField)
			{
				if (field.getName().equalsIgnoreCase("id"))
				{
					sId = field;
					sId.setAccessible(true);
				}
				else if (field.getName().equalsIgnoreCase("name"))
				{
					sName = field;
					sName.setAccessible(true);
				}
			}
			final Collection<?> values = (Collection<?>) attribute;
			Iterator<?> iterator = values.iterator();
			if (iterator.hasNext())
			{
				selectSb.append("<select name='" + selId + "'>");
				if (id.equals("-1"))
					selectSb.append("<option value='-1'>查询全部</option>");
				while (iterator.hasNext())
				{
					Object value = iterator.next();
					if (id.equals("-1"))
					{
						if (selObjectId != null && selObjectId.equals(sId.get(value)))
						{
							selectSb.append("<option value='" + sId.get(value) + "' selected>")
									.append(sName.get(value)).append("</option>\r\n");
						}
						else
						{
							selectSb.append("<option value='" + sId.get(value) + "'>").append(sName.get(value))
									.append("</option>\r\n");
						}

					}
					else
					{
						if (id != null && id.equals(sId.get(value)))
						{
							selectSb.append("<option value='" + sId.get(value) + "'>").append(sName.get(value))
									.append("</option>\r\n");
						}
					}
				}
				selectSb.append("</select>");
			}
		}
		return selectSb.toString();
	}

}
